package service

import (
	"demo/basic/config"
	"demo/handler/model"
	"demo/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddBlack(c *gin.Context) {
	var form request.AddBlack
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "参数错误"})
		return
	}
	userId := c.GetFloat64("userId")
	var black model.Black
	if err := black.IsBlack(config.DB, int(userId), form.FriendId); err == nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "好友已存在黑名单"})
		return
	}
	black = model.Black{
		UserId:   int(userId),
		FriendId: form.FriendId,
	}
	if err := black.AddBlack(config.DB); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "加入黑名单失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "加入黑名单成功",
	})
	return
}

func GetBlack(c *gin.Context) {
	userId := c.GetFloat64("userId")
	var black model.Black
	list, err := black.GetBlack(config.DB, int(userId))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "查询黑名单列表失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查询黑名单列表成功",
		"data": gin.H{
			"list": list,
		},
	})
	return

}
